package cn.zswltech.gruul.web.api.controller;

import cn.hutool.core.util.StrUtil;
import cn.zswltech.gruul.api.UserApi;
import cn.zswltech.gruul.biz.service.UserService;
import cn.zswltech.gruul.common.constant.UserConstants;
import cn.zswltech.gruul.common.result.Response;
import cn.zswltech.gruul.web.api.util.AccountUtil;
import cn.zswltech.gruul.common.entity.UserDO;
import cn.zswltech.gruul.common.dal.query.UserQuery;
import cn.zswltech.gruul.common.dal.vo.GruulDictionaryDTO;
import cn.zswltech.gruul.common.dal.vo.UserVO;
import cn.zswltech.gruul.web.api.controller.common.BaseController;
import cn.zswltech.gruul.common.param.UserParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
public class UserController extends BaseController implements UserApi {

    @Resource(name = "userServiceAPI")
    private UserService userService;

    /**
     * 查询用户列表,带分页
     *
     * @param query
     * @return
     * @
     */
    @Override
    public Response<Map<String, Object>> queryUser(UserQuery query) {
        return userService.queryUser(query);
    }

    /**
     * 查询所有账号和用户中文名的映射关系，前端替换、下拉框等地方使用
     * @return map key=account, value=username
     */
    @Override
    public Response<List<GruulDictionaryDTO>> accountUsernameMappingList(@RequestParam(required = false) String orgCode){
        return userService.accountUsernameMappingList(orgCode);
    }

    /**
     * 查询用户信息
     *
     * @return
     * @
     */
    @Override
    public Response<UserVO> queryUser() {
        return userService.getUserInfo();
    }

    /**
     * 创建用户
     *
     * @param userParam
     * @return
     * @
     */
    @Override
    public Response createUser(@RequestBody UserParam userParam) {
        UserDO userDO = UserParam.genUserDO(userParam);
        userDO.setAccount(userParam.getAccount());
        // 初始化参数
        String pwd = userParam.getPwd();
        String salt = AccountUtil.genSalt();
        userDO.setSalt(salt);
        if (StringUtils.isBlank(pwd)) {
            //目前前端不会传pwd进来，仅此密码是用默认密码
            pwd = UserConstants.DEFAULT_PWD;
        }

        userDO.setPwd(AccountUtil.genPwd(pwd, salt));
        return userService.createUser(userDO,userParam.getOrgRoles());
    }

    /**
     * 更新用户
     *
     * @param userParam
     * @return
     * @
     */
    @Override
    public Response updateUser(@RequestBody UserParam userParam) {
        UserDO userDO = UserParam.genUserDO(userParam);
        userDO.setId(userParam.getId());
        if (StrUtil.isNotEmpty(userDO.getPhone()) && userDO.getPhone().contains("****")) {
            userDO.setPhone(null);
        }
        return userService.updateUser(userDO,userParam.getOrgRoles(),true);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     * @
     */
    @Override
    public Response deleteUser(@RequestParam("id") Long id) {
        return userService.deleteUser(id);
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     * @
     */
    @Override
    public Response resetPwd(@RequestParam("id") Long id, HttpServletResponse httpServletResponse) {
        return userService.resetPwd(id, httpServletResponse);
    }

    /**
     * 启动/禁用用户
     *
     * @param id
     * @return
     * @
     */
    @Override
    public Response status(@RequestParam("id") Long id, @RequestParam("status") int status) {
        UserDO userDO = new UserDO();
        userDO.setId(id);
        userDO.setStatus(status);
        return userService.updateUser(userDO, null, false);
    }


}
